Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.IO

Public Class ParticipantDlg
    Const instructionTemplate As String = "Thank you for participating in the clinical research." & vbCrLf & vbCrLf & _
    "Participant Details" & vbCrLf & "===================" & vbCrLf & "Participant Name: {0}" & vbCrLf & _
    "Date Registered: {1}" & vbCrLf & "Application Specific Participant Id: {2}" & vbCrLf & " " & vbCrLf & _
    "Please visit the following HealthVault shell url to login and use the participant " & vbCrLf & _
    "identity code below to enable this clinical research to access your healthvault data. " & vbCrLf & _
    "You will be asked the answer to the security question during that process." & vbCrLf & vbCrLf & _
    "Url: https://account.healthvault-ppe.com/patientwelcome.aspx" & vbCrLf & "Participant Identity Code: {3}" & vbCrLf & _
    "Security Question: {4}" & vbCrLf & "Security Answer: Please make sure you remember the answer"

    Dim _appSpecificParticipantId As String

    Public Property AppSpecificParticpantId() As String
        Get
            Return _appSpecificParticipantId
        End Get
        Set(ByVal value As String)
            _appSpecificParticipantId = value
        End Set
    End Property

    Dim _myReader As StringReader = Nothing

    Private Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim row As DataRow = XmlStore.FetchParticipantFromLocalStore(_appSpecificParticipantId)
        If (row IsNot Nothing) Then
            Dim instructions As String = String.Format(instructionTemplate, _
                    row("ParticipantName"), _
                    row("DateTokenGenerated"), _
                    row("MyAppSpecificParticipantId"), _
                    row("ParticipantCode"), _
                    row("SecurityQuestion"))

            txtDetails.Text = instructions
        End If
    End Sub

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        printDialog1.Document = printDocument1
        Dim strText As String = Me.txtDetails.Text
        _myReader = New StringReader(strText)
        If printDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Me.printDocument1.Print()
        End If
    End Sub

    Private Sub PrintDocument_PrintPage(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs) Handles printDocument1.PrintPage
        Dim linesPerPage As Single = 0
        Dim yPosition As Single = 0
        Dim count As Integer = 0
        Dim leftMargin As Single = ev.MarginBounds.Left
        Dim topMargin As Single = ev.MarginBounds.Top
        Dim line As String = Nothing
        Dim printFont As Font = Me.txtDetails.Font
        Dim myBrush As SolidBrush = New SolidBrush(Color.Black)

        'Work out the number of lines per page, using the MarginBounds.
        linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics)

        'Iterate over the string using the StringReader, printing each line.
        While count < linesPerPage AndAlso ((line = _myReader.ReadLine()) <> Nothing)
            'calculate the next line position based on 
            'the height of the font according to the printing device
            yPosition = topMargin + (count * printFont.GetHeight(ev.Graphics))

            'draw the next line in the rich edit control
            ev.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, New StringFormat())
            count += 1
        End While

        'If there are more lines, We need print another page.
        'Since _myReader is global to this class, we just need to set the
        'PrintPageEventArgs's HasMorePages to true so that this function will be called again
        'and the remaining lines in _myReader is sent to printer

        If line IsNot Nothing Then
            ev.HasMorePages = True
        Else
            ev.HasMorePages = False
        End If

        myBrush.Dispose()
    End Sub
End Class